/*
 * @Author: ellery
 * @Date: 2022-08-31 18:00:28
 * @LastEditTime: 2022-09-01 00:06:10
 * @LastEditors: ellery
 * @Description: 数据处理，如时间格式化，金额转化等
 * @FilePath: \vue2-eslint-model\src\common\processingData.js
 * Copyright (c) 2022 by ellery, All Rights Reserved.
 * 愿天堂没代码
 */
/**
 * @description: 时间格式化
 * @param {String} time 需要带上时分秒,如："2022-08-01 10:01:01"
 * @param {String} cFormat 格式化 y年，m月，d日，a星期,h时,i分,s秒
 * @return {String}
 */
import RR from "./regular.js";
export const formatDate = function(time, cFormat) {
  const date = new Date(time);
  if (arguments.length === 0 || date == "Invalid Date") return;
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
};

/**
 * @description: 格式化 分整数 为 保留两位小数
 * @param {String} val
 * @return {Number}
 */
export const toPrice = function(val) {
  val = Number(val);
  if (isNaN(val)) {
    val = 0;
  }
  const price = (val * 0.01).toFixed(2);
  return price;
};
/**
 * @description: 加法函数:javascript的加法结果会有误差，在两个浮点数相加的时候会比较明显。这个函数返回较为精确的加法结果。调用：accAdd(arg1,arg2)
 * @param {String | Number} arg1
 * @param {String | Number} arg2
 * @return {String}
 */
export const accAdd = function(arg1, arg2) {
  let r1 = 0;
  let r2 = 0;
  try {
    r1 = arg1.toString().split(".")[1].length;
  } catch (e) {
    r1 = 0;
  }
  try {
    r2 = arg2.toString().split(".")[1].length;
  } catch (e) {
    r2 = 0;
  }
  const c = Math.abs(r1 - r2);
  const m = Math.pow(10, Math.max(r1, r2));
  if (c > 0) {
    const cm = Math.pow(10, c);
    if (r1 > r2) {
      arg1 = Number(arg1.toString().replace(".", ""));
      arg2 = Number(arg2.toString().replace(".", "")) * cm;
    } else {
      arg1 = Number(arg1.toString().replace(".", "")) * cm;
      arg2 = Number(arg2.toString().replace(".", ""));
    }
  } else {
    arg1 = Number(arg1.toString().replace(".", ""));
    arg2 = Number(arg2.toString().replace(".", ""));
  }
  return (arg1 + arg2) / m;
};
/**
 * @description: 换行符切割为数组
 * @param {String} str
 * @return {Array}
 */
export const lineFeed = function(str) {
  const arr = str.split(RR.lineFeed);
  arr.map((item, index) => {
    if (!item) {
      arr.splice(index, 1); // 删除空项
    }
  });
  return arr;
};
/**
 * @description: 防抖
 * @param {Function} fn 函数
 * @param {Number} delay 延迟*秒
 * @return {*}
 */
let t = null;
export const debounce = function(fn, delay) {
  if (t !== null) {
    clearTimeout(t);
  }
  t = setTimeout(() => {
    fn();
  }, delay);
};
